package ua.kpi_java_training.zastupaylo.module1;

import org.junit.Test;
import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;

/**
 * Tests for PalindromeFinder.class
 *
 * @version 1.0
 * @author Zastupaylo Alexander (zastupaylo@gmailcom)
 */
public class PalindromeFinderTest {

    @Test
    public void testInput() throws Exception {
        //Found this unit test for input, but not sure that it's what we need.
        //I'll ask this question on Friday

        /*String data = "ovovokak\n";
        InputStream stdin = System.in;
        try {
            System.setIn(new ByteArrayInputStream(data.getBytes()));
            Scanner scanner = new Scanner(System.in);
            System.out.println(scanner.nextLine());
        } finally {
            System.setIn(stdin);
        }*/
    }

    @Test
    public void testFindPalindrome() throws Exception {
        String testInputPhraseWithoutPalindroms = "intellij idea";
        ArrayList<String> testPalindromsEmpty = new ArrayList<String>() {{
            add("No palindromes found.");
        }};
        PalindromeFinder.findPalindrome(testInputPhraseWithoutPalindroms);
        assertEquals(PalindromeFinder.palindromes, testPalindromsEmpty);
        PalindromeFinder.palindromes.clear();

        String testInputPhraseWithPalindroms = "ovovokak";
        ArrayList<String> testPalindroms = new ArrayList<String>() {{
            add("ovo");
            add("vov");
            add("ovo");
            add("kak");
            add("ovovo");
        }};
        PalindromeFinder.findPalindrome(testInputPhraseWithPalindroms);
        assertEquals(PalindromeFinder.palindromes, testPalindroms);
        PalindromeFinder.palindromes.clear();
    }

    @Test
    public void testFindAllSpecifiedLengthPalindromes() throws Exception {
        String testInputPhraseWithPalindroms = "ovovokak";
        int specifiedLength = 3;
        ArrayList<String> testPalindroms = new ArrayList<String>() {{
            add("ovo");
            add("vov");
            add("ovo");
            add("kak");
        }};
        PalindromeFinder.findAllSpecifiedLengthPalindromes(specifiedLength, testInputPhraseWithPalindroms);
        assertEquals(PalindromeFinder.palindromes, testPalindroms);
        PalindromeFinder.palindromes.clear();
    }

    @Test
    public void testIsPalindrom() throws Exception {
        String palindrom = "ovovo";
        assertTrue(PalindromeFinder.isPalindrom(palindrom));

        String notPalindrom = "idea";
        assertFalse(PalindromeFinder.isPalindrom(notPalindrom));
    }

    @Test
    public void testOutput() throws Exception {
        PalindromeFinder.palindromes = new ArrayList<String>() {{
            add("ovo");
            add("vov");
            add("ovo");
            add("kak");
            add("ovovo");
        }};
        String expectedOutput = "List of palindromes:\novo\nvov\novo\nkak\novovo\n";
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        System.setOut(new PrintStream(baos));
        PalindromeFinder.output();
        baos.flush();
        String actualOutput = new String(baos.toByteArray());
        assertEquals(expectedOutput, actualOutput);
    }
}
